﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows;
using System.Reflection;

namespace com.gui.effects.minToTray
{
    public class MinToTray
    {
        /* Private variables */
        private Window _window;
        private NotifyIcon _notifyIcon;
        private Icon _customIcon;
        private bool _balloonShown;
        private static string _balloonTitle;
        private static string _balloonToolTip;
        /* Public items */
        public static string balloonTitle
        { 
          get { return _balloonTitle; } 
          set { _balloonTitle = value; } 
        }
        public static string balloonToolTip
        {
            get { return _balloonToolTip; }
            set { _balloonToolTip = value; } 
        }

        /// <summary>
        /// Enable Minimize to tray effect
        /// </summary>
        /// <param name="window">Window on which the effect is applied</param>
         public static void Enable(Window window)
         {
             new MinToTray(window);
         }
        /// <summary>
        /// Enable Minimize to tray effect with custom trayIcon
        /// </summary>
        /// <param name="window">Window on which the effect is applied</param>
        /// <param name="notifyIcon">Notification Icon</param>
         public static void Enable(Window window, Icon notifyIcon)
         {
             new MinToTray(window,notifyIcon);
         }
        /// <summary>
        /// Enable Minimize to tray effect with custom trayIcon file
        /// </summary>
        /// <param name="window">Window on which the effect is applied</param>
        /// <param name="fileName">Notification Icon file name</param>
         public static void Enable(Window window, string fileName)
         {
             new MinToTray(window, fileName);
         }
        /// <summary>
        /// Set Balloon title
        /// </summary>
        /// <param name="BalloonTitle">Balloon title to be set</param>
         public static void setBalloonTitle(string BalloonTitle)
         {
             balloonTitle = BalloonTitle;
         }
        /// <summary>
        /// Set Balloon Tool Tip 
        /// </summary>
         /// <param name="BalloonToolTip">Balloon tool tip to be set</param>
         public static void setBalloonToolTip(string BalloonToolTip)
         {
             balloonToolTip = BalloonToolTip;
         }
        /// <summary>
        /// get Minimize to tray object. 
        /// </summary>
        /// <returns>get Minimize to tray instance</returns>
        public MinToTray getMinToTray()
        {
            return this;
        }

        /* Private contructor to limit instantiation. */
        private MinToTray(Window window)
        {
            _window = window;
            balloonTitle = window.Title;
            // need a code patch here
            balloonToolTip = window.Title;
            _window.StateChanged += new EventHandler(HandleStateChanged);
        }
        /* Private contructor to limit instantiation. */
        private MinToTray(Window window, Icon notifyIcon) : this(window)
        {
            try
            {
                _customIcon = notifyIcon;
            }
            catch 
            {
                // fall back using null
                _customIcon = null;
            }
        }
        /* Private contructor to limit instantiation. */
        private MinToTray(Window window,string fileName) : this(window)
        {
            try
            {
                _customIcon = new System.Drawing.Icon(fileName);
            }
            catch
            {
                // Fall back with null.
                _customIcon = null;
            }
        }
       
        private void HandleStateChanged(object sender, EventArgs e)
        {
            if (_notifyIcon == null)
            {
                _notifyIcon = new NotifyIcon();

                /*dont need this code below done entirely for debugging */
                try
                {
                    if (_customIcon != null)
                    {
                        _notifyIcon.Icon = _customIcon;
                    }
                    else
                    {
                        // Fall back using default windows icon.
                        _notifyIcon.Icon = SystemIcons.WinLogo;
                    }
                }
                /* for installed application */
                catch(Exception ex)
                {
                    Console.WriteLine("Error while setting notification icon :" + ex);
                   // _notifyIcon.Icon = Asianet.Ico_Resource.Asianet_logo;
                   // _notifyIcon.Icon = new System.Drawing.Icon(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Resources\Asianet_logo.ico");
                }

                _notifyIcon.Visible = true;
                _notifyIcon.MouseClick   += new MouseEventHandler(HandleNotifyIconOrBalloonClicked);
                _notifyIcon.BalloonTipClicked += new EventHandler(HandleNotifyIconOrBalloonClicked);
            }
         
            _notifyIcon.Text = _window.Title;

            // Show/hide Window and NotifyIcon
            var minimized = (_window.WindowState == WindowState.Minimized);
            _window.ShowInTaskbar = !minimized;
            _notifyIcon.Visible = minimized;

            if (minimized && !_balloonShown)
            {
                // If this is the first time minimizing to the tray, show the user what happened
                _notifyIcon.ShowBalloonTip(1000, balloonTitle, balloonToolTip, ToolTipIcon.Info);
                _balloonShown = true;
            }
        }
        private void HandleNotifyIconOrBalloonClicked(object sender, EventArgs e)
        {
            // Restore the Window
            _window.WindowState = WindowState.Normal;
        }
    }
}
